home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Turnbull China Bikeride
/
Turnbull China Bikeride - Disc 1.iso
/
ARGONET
/
PD
/
DTP
/
PDFENCRY.SPK
/
encryption_d
next >
Wrap
Text File
|
1998-04-13
|
10KB
|
365 lines
diff ../xpdf-0.7a/xpdf/Makefile xpdf/Makefile
32c32,33
< XOutputDev.o XRef.o xpdf.o
---
> XOutputDev.o XRef.o xpdf.o md5.o rc4.o
>
46c47,48
< Parser.o PDFDoc.o PSOutputDev.o Stream.o XRef.o pdftops.o
---
> Parser.o PDFDoc.o PSOutputDev.o Stream.o XRef.o pdftops.o md5.o rc4.o
>
57c59,60
< Parser.o PDFDoc.o TextOutputDev.o Stream.o XRef.o pdftotext.o
---
> Parser.o PDFDoc.o TextOutputDev.o Stream.o XRef.o pdftotext.o md5.o rc4.o
>
diff ../xpdf-0.7a/xpdf/Parser.cc xpdf/Parser.cc
18a19,20
> #include "md5.h"
> #include "XRef.h"
160a163,198
> }
>
> Object *Parser::getEncryptedObj(Object *obj, int num, int gen) {
> Stream *str1, *str2;
> MD5 context;
> RC4KEY rc4Key;
> GString MD5Key;
> GString Key;
>
> getObj(obj);
> // check for obj type
> if (obj->isStream()) {
> // enable Decryption on base File Stream
> str2 = obj->getStream();
> do {
> str1 = str2;
> str2 = str1->getBaseStream();
> } while (str1 != str2);
>
> Key.append(xref->getEncryptionKey());
> Key.append((char)( num & 0xff));
> Key.append((char)((num >> 8) & 0xff));
> Key.append((char)((num >>16) & 0xff));
> Key.append((char)( gen & 0xff));
> Key.append((char)((gen >> 8) & 0xff));
> context.update((unsigned char *)Key.getCString(), 10);
> context.finalize();
> MD5Key.clear();
> MD5Key.append((char *)context.raw_digest(), 10);
>
> // determine rc4Key
> rc4ExpandKey(&rc4Key, (unsigned char *)MD5Key.getCString(), 10);
>
> str1->enableEncryption(&rc4Key);
> }
> return obj;
diff ../xpdf-0.7a/xpdf/Parser.h xpdf/Parser.h
32a33
> Object *getEncryptedObj(Object *obj, int num, int gen);
diff ../xpdf-0.7a/xpdf/Stream.cc xpdf/Stream.cc
217a218
> encryption = gFalse;
245a247,249
> if (encryption == gTrue) {
> rc4Crypt(&rc4Key, (unsigned char *)buf, n);
> }
294a299,307
>
>
> void FileStream::enableEncryption(RC4KEY *prc4Key)
> {
> // prepary RC4 key
> rc4Key = *prc4Key;
> encryption = gTrue;
> }
>
diff ../xpdf-0.7a/xpdf/Stream.h xpdf/Stream.h
17a18
> #include <Error.h>
20a22,25
> //extern "C" {
> #include "rc4.h"
> //}
>
77a83,84
> virtual void enableEncryption(RC4KEY *rc4Key) {error(-1, "No FileStream, no decryption");};
>
112a120,121
> void enableEncryption(RC4KEY *rc4Key);
>
113a123,124
> RC4KEY rc4Key;
> GBool encryption;
diff ../xpdf-0.7a/xpdf/XRef.cc xpdf/XRef.cc
24a25,28
> #include "md5.h"
> //extern "C" {
> #include "rc4.h"
> //}
48a53
> encrypted = gFalse;
93c98,102
< // check for encryption
---
> m_okToPrint = gTrue;
> m_okToCopy = gTrue;
> m_okToChange = gTrue;
> m_okToAddNotes = gTrue;
>
95,97c104,108
< ok = gFalse;
< xref = oldXref;
< return;
---
> if (setupDecryption() == gFalse) {
> ok = gFalse;
> xref = oldXref;
> return;
> }
103a115
> encryptionDict.free();
302a315
>
383,393c396,397
< Object obj;
< GBool encrypted;
<
< trailerDict.dictLookup("Encrypt", &obj);
< if ((encrypted = !obj.isNull())) {
< error(-1, "PDF file is encrypted and cannot be displayed");
< error(-1, "* Decryption support is currently not included in xpdf");
< error(-1, "* due to legal restrictions: the U.S.A. still has bogus");
< error(-1, "* export controls on cryptography software.");
< }
< obj.free();
---
> trailerDict.dictLookup("Encrypt", &encryptionDict);
> encrypted = encryptionDict.isDict();
398c402
< return gTrue;
---
> return m_okToPrint;
402c406,414
< return gTrue;
---
> return m_okToCopy;
> }
>
> GBool XRef::okToChange() {
> return m_okToChange;
> }
>
> GBool XRef::okToAddNotes() {
> return m_okToAddNotes;
427c439,443
< parser->getObj(obj);
---
> if (encrypted) {
> parser->getEncryptedObj(obj, num, gen);
> } else {
> parser->getObj(obj);
> }
438a455,614
> }
>
> GBool XRef::setupDecryption() {
> Object obj;
> GBool encrypted;
> GBool passwordOk;
> GString userPassword;
>
> // check filter
> encryptionDict.dictLookupNF("Filter", &obj);
> if (!obj.isName()) {
> error(-1, "No filter specified, asume Standard filter");
> } else if (strcmp(obj.getName(), "Standard") != 0) {
> error(-1, "File is encrypted with a non Standard Filter.\
> xpdf supports only the standard encryption filter");
> obj.free();
> return gFalse;
> }
> obj.free();
>
> // check for no user password
> userPassword.clear();
> if (checkUserPassword(&userPassword) == gFalse) {
> // ask for user password
> // ...
> // check with user password
> error(-1, "pdf file is encrypted with a user password.\
> xpdf does not support passwords please contact the author of the pdf file\
> and ask him for a non password encrypted version");
> return gFalse;
> }
>
> // set permissions
> // permissions
> encryptionDict.dictLookupNF("P", &obj);
> if (obj.isNull() || !obj.isInt()) {
> error(-1, "No permissions specified");
> return gFalse;
> }
> int Permissions = obj.getInt();
> obj.free();
> m_okToPrint = Permissions & 0x04 ? gTrue : gFalse;
> m_okToChange = Permissions & 0x08 ? gTrue : gFalse;
> m_okToCopy = Permissions & 0x10 ? gTrue : gFalse;
> m_okToAddNotes = Permissions & 0x20 ? gTrue : gFalse;
>
> return gTrue;
> }
>
> GBool
> XRef::checkUserPassword(GString *userPassword)
> {
> GString preparedPassword;
> RC4KEY rc4Key;
> Object obj;
> char localPassword[32];
>
> if (preparePassword(userPassword, &preparedPassword) == gFalse) {
> return gFalse;
> }
> if (MakeEncryptionKey(&preparedPassword, &encryptionKey) == gFalse) {
> return gFalse;
> }
>
> // get the User password
> encryptionDict.dictLookupNF("U", &obj);
> if (obj.isNull() || !obj.isString()) {
> error(-1, "No user password specified");
> return gFalse;
> }
> memcpy(localPassword, obj.getString()->getCString(), 32);
> obj.free();
>
> // prepary RC4 key
> rc4ExpandKey(&rc4Key, (unsigned char *)encryptionKey.getCString(), 5);
>
> // encrypt User password
> rc4Crypt(&rc4Key, (unsigned char *)&localPassword[0], 32);
>
> // compare preparedPassword and decryptedPassword
> for (int i=0; i<32; i++) {
> if (localPassword[i] != preparedPassword.getChar(i)) {
> // not equal
> return gFalse;
> }
> }
> // password OK
>
> return gTrue;
> }
>
> GBool
> XRef::MakeEncryptionKey(GString *password, GString *encryptionKey)
> {
> MD5 context;
> Object obj, obj1;
> unsigned char Pkey[4];
>
> context.update((unsigned char *)password->getCString(), 32);
>
> // others
> // owner key
> encryptionDict.dictLookupNF("O", &obj);
> if (obj.isNull() || !obj.isString()) {
> error(-1, "No owner password specified");
> return gFalse;
> }
> context.update((unsigned char *)obj.getString()->getCString(), 32);
> obj.free();
>
> // permissions
> encryptionDict.dictLookupNF("P", &obj);
> if (obj.isNull() || !obj.isInt()) {
> error(-1, "No permissions specified");
> return gFalse;
> }
> int Permissions = obj.getInt();
> Pkey[0] = Permissions & 0xff;
> Pkey[1] = (Permissions >> 8 ) & 0xff;
> Pkey[2] = (Permissions >> 16) & 0xff;
> Pkey[3] = (Permissions >> 24) & 0xff;
> context.update(&Pkey[0], 4);
> obj.free();
>
> // first element from ID
> trailerDict.dictLookupNF("ID", &obj);
> if (obj.isNull() || !obj.isArray()) {
> error(-1, "No ID specified");
> return gFalse;
> }
> obj.arrayGet(0, &obj1);
> if (obj1.isNull() || !obj1.isString()) {
> error(-1, "No ID elements specified");
> return gFalse;
> }
> context.update((unsigned char *)(obj1.getString()->getCString()), (int)(obj1.getString()->getLength()));
> obj1.free();
> obj.free();
>
> context.finalize();
>
> encryptionKey->clear();
> encryptionKey->append((char *)context.raw_digest(), 5);
>
> return gTrue;
> }
>
> GBool
> XRef::preparePassword(GString *password, GString *preparedPassword)
> {
> static char acFill[32] = {0x28, 0xbf, 0x4e, 0x5e, 0x4e, 0x75, 0x8a, 0x41,
> 0x64, 0x00, 0x4e, 0x56, 0xff, 0xfa, 0x01, 0x08,
> 0x2e, 0x2e, 0x00, 0xb6, 0xd0, 0x68, 0x3e, 0x80,
> 0x2f, 0x0c, 0xa9, 0xfe, 0x64, 0x53, 0x69, 0x7a};
>
> preparedPassword->clear();
> preparedPassword->append(password);
> preparedPassword->append(&acFill[0], 32);
>
> return gTrue;
diff ../xpdf-0.7a/xpdf/XRef.h xpdf/XRef.h
47a48,49
> GBool okToChange();
> GBool okToAddNotes();
54a57,59
> // get encryption key
> GString *getEncryptionKey() { return &encryptionKey; };
>
69a75,87
>
> GBool m_okToPrint;
> GBool m_okToCopy;
> GBool m_okToChange;
> GBool m_okToAddNotes;
> Object encryptionDict; // encryption dictionary
> GBool encrypted;
> GString encryptionKey;
>
> GBool setupDecryption();
> GBool checkUserPassword(GString *userPassword);
> GBool MakeEncryptionKey(GString *password, GString *encryptionKey);
> GBool preparePassword(GString *password, GString *preparedPassword);
diff ../xpdf-0.7a/xpdf/config.h xpdf/config.h
17c17
< #define xpdfVersion "0.7a"
---
> #define xpdfVersion "0.7a (encryption)"
24a25
> #define xpdfCopyright_Encryption "Encryption by Leo J.B. Smiers"
diff ../xpdf-0.7a/xpdf/pdftops.cc xpdf/pdftops.cc
63a64
> fprintf(stderr, "%s\n", xpdfCopyright_Encryption);
diff ../xpdf-0.7a/xpdf/pdftotext.cc xpdf/pdftotext.cc
61a62
> fprintf(stderr, "%s\n", xpdfCopyright_Encryption);
diff ../xpdf-0.7a/xpdf/xpdf.cc xpdf/xpdf.cc
315a316
> fprintf(stderr, "%s\n", xpdfCopyright_Encryption);
355a357
> fprintf(stderr, "%s\n", xpdfCopyright_Encryption);